<?php
namespace MLM\Controller;

use Zend\View\Model\ViewModel;
use Zend\Authentication\Result;
use Zend\Authentication\AuthenticationService;

class IndexController extends \MLM\Model\Generic\Controller {
    
    public function indexAction() {
        $identity = $this->hasSession();
        
        if ($identity->member_type_id == 4)
            $this->layout('layout/admin');
        else
            $this->layout('layout/member');

        
        return new ViewModel();
    }

    public function custcareAction() {

        $identity = $this->hasSession();
        $this->layout('layout/member');


        $view = new ViewModel();

        //Get Messages Notif
        $this->getMsgNotif($identity->member_type_id, $identity->member_id);
        $this->layout()->navMenu = "<li class='active'>Customer Care </li>";

        return $view;
    }


    public function jqgridAction() {
        $view = new ViewModel();
        $view->setTerminal(true);
        return $view;
    }

    //Get Messages Notif
    private function getMsgNotif($memberTypeId, $memberId){


        if ($memberId == 100) $memberId2 = $memberId;
        else{
            $identity = $this->hasSession();
            //$memberId2 = ($identity->member_reff_id == "") ? $memberId : $identity->member_reff_id;
            $memberId2 = $memberId;
            $isAgent = $identity->member_reff_id;
        }


        $notifStorage = new \MLM\Model\Notification\Storage($this->_db);
        $notifMsg = new \MLM\Model\Notification($notifStorage);
        $newMsgCnt = $notifMsg->getCountNewMsg($memberId);
        $newMsgList = $notifMsg->getNewMsgList($memberId);
        $newAlertCnt = $notifMsg->getCountAlertApproval($memberTypeId, $memberId2, $isAgent);
        $newAlertList = $notifMsg->getAlertApproval($memberTypeId, $memberId2, $isAgent);

        $action = $this->getEvent()->getRouteMatch()->getParam('action');
        $control = $this->getEvent()->getRouteMatch()->getParam('controller');

        $this->layout()->menu = $control.'\\'.$action;
        $this->layout()->newMsgCnt = $newMsgCnt;
        $this->layout()->newMsgList = $newMsgList;
        $this->layout()->newAlertCnt = $newAlertCnt;
        $this->layout()->newAlertList = $newAlertList;

    }
    
    private function hasSession() {
        $auth = new AuthenticationService();

        $identity = $auth->getIdentity();
		//print_r($identity);
        if (!$auth->hasIdentity()) {
			//echo "gak ada session";
            return $this->redirect()->toRoute('application/default', array('controller' => 'Index', 'action' => 'login'));
        } else {
            return $auth->getIdentity();
        }
    }

}
